from pylab import *
import scipy.stats
import os
import sys

if __name__ == "__main__":
    
    
    if not os.path.exists("../durations.dat") or not os.path.exists("../blocks.dat") or not os.path.exists("../files.dat"):
        print "Inexistent data files!!!"
        sys.exit(1)
    
    #Reading block data
    handle = open("../blocks.dat", "r")
    blocksMap = {}
    for line in handle.readlines():
        client, time, total = line.split()
        clientData = blocksMap.get(client)
        if clientData == None:
            clientData = [[], []]
            blocksMap[client] = clientData
        
        clientData[0].append(time)
        clientData[1].append(total) 
    
    handle.close()
    
    #Reading file data
    handle = open("../files.dat", "r")
    filesMap = {}
    for line in handle.readlines():
        client, time, total = line.split()
        clientData = filesMap.get(client)
        if clientData == None:
            clientData = [[], []]
            filesMap[client] = clientData
        
        clientData[0].append(time)
        clientData[1].append(total)     
    handle.close()
    
    #Reading duration data
    handle = open("../durations.dat","r")
    durationsMap = {}
    for line in handle.readlines():
        client, file, duration = line.split()
        clientData = durationsMap.get(client)
        if clientData == None:
            clientData = {}
            durationsMap[client] = clientData
        
        fileData = clientData.get(file)
        if fileData == None:
            fileData = []
            clientData[file] = fileData
        
        fileData.append( float(duration) )
    
    handle.close()
            
           
    figure(figsize = (20, 8))

    #Creating block graphic
    f = subplot(311)
    maxTime = 0
    maxNumber = 0
    title("Blocks and files counting per time")
    
    for client, data in blocksMap.iteritems():
        plot(data[0], data[1], "ro-")
        xlabel('Time')
        ylabel("Number of blocks")
        
        maxCurrentTime = max([float(v) for v in data[0]])
        maxCurrentNumber = max([float(v) for v in data[1]])
        
        if maxTime == 0 or float(maxCurrentTime) > maxTime:
            maxTime = float(maxCurrentTime)
        if maxNumber == 0 or float(maxCurrentNumber) > maxNumber:
            maxNumber = float( maxCurrentNumber)    
    
    axis([0, maxTime+10, -1, maxNumber+10.0])

    f.grid(True)
    
    #Creating file graphic
    f = subplot(312)
    maxTime = 0
    maxNumber = 0
    
    for client, data in filesMap.iteritems():
        plot(data[0], data[1], "r^-")
        xlabel('Time')
        ylabel("Number of files")
        
        maxCurrentTime = max([float(v) for v in data[0]])
        maxCurrentNumber = max([float(v) for v in data[1]])
        
        if maxTime == 0 or float(maxCurrentTime) > maxTime:
            maxTime = float(maxCurrentTime)
        if maxNumber == 0 or float(maxCurrentNumber) > maxNumber:
            maxNumber = float(maxCurrentNumber)    
    
    axis([0, maxTime+10, -1, maxNumber+10.0])

    f.grid(True)
    
    #Creating filesDurations graphic
    f = subplot(313)
    maxDuration = 0
    
    durationsMeanPerFile = {}
    for client, filesDurations in durationsMap.iteritems():
        for file, durationsValues in filesDurations.iteritems():
        
            maxCurrentDuration = max(durationsValues)
            durationsMean = scipy.stats.mean(durationsValues)
            
            if durationsMean > maxDuration:
                maxDuration = durationsMean
            
            #Getting previous data
            clientData = durationsMeanPerFile.get(client)
            if clientData == None:
                clientData = {}
                durationsMeanPerFile[client] = clientData
            
            clientData[file] = durationsMean    
    
    print str(durationsMeanPerFile)
    
    #Sorting durations of files for all clients
    allFiles = set([])
    for client, clientData in durationsMeanPerFile.iteritems():
        clientFiles = clientData.keys()
        for file in clientFiles:
            allFiles.add(file)
        
    clientsMeans = {}
    for file in allFiles:
        for client, clientMap in durationsMeanPerFile.iteritems():
            clientData = clientsMeans.get(client)
            if clientData == None:
                clientData = []
                clientsMeans[client] = clientData
            
            durationMean = clientMap.get(file, 0.0)
            clientData.append(durationMean)    
    
    #Creating graphic
    f = subplot(313)
    for client, filesDurationsMeans in clientsMeans.iteritems():
        plot(filesDurationsMeans, "r--")
    xlabel("File")
    ylabel("Mean response time")
    axis([0, 5, -1, maxDuration+1])
    
    #Saving figure
    savefig("filesBlock")